package dq.study.concurrent.queue.blocking;

import com.sun.istack.internal.NotNull;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author dongqun
 * @date 2020/7/6 15:55
 */
public class DelayItem implements Delayed {

    private Long time;

    private String command;

    public DelayItem(Long time, String command) {
        this.time = time;
        this.command = command;
    }

    @Override
    public long getDelay(TimeUnit unit) {
//        System.out.println(time - System.nanoTime());
//        System.out.println(unit.convert(time - System.nanoTime(), TimeUnit.NANOSECONDS));
        return unit.convert(time - System.nanoTime(), TimeUnit.NANOSECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        DelayItem other = (DelayItem) o;
        if (this.time > other.time) return 1;
        else if (this.time < other.time) return -1;
        return 0;
    }

    public void execute() {
        System.out.println(command);
    }
}
